﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;

namespace CTEA_INDIA
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;
           // PopulateProductDetail();

            if (Session["UserID"] == null)
            {
                Response.Redirect("Default.aspx");
            }

            //int userTypeId = Convert.ToInt32(Session["UserTypeId"]);

            //if (!IsPostBack)
            //{

            //    if (Session["UserTypeId"].ToString() == "3")
            //    {
            //        gdProductDetail.Columns[1].Visible = false;
            //        gdProductDetail.Columns[2].Visible = false;
            //        gdProductDetail.Columns[5].Visible = false;
            //        gdProductDetail.Columns[6].Visible = false;

            //        PopulateProductDetail();

            //    }
            //    if (Session["UserTypeId"].ToString() == "1")
            //    {

            //        PopulateProductDetail();
            //    }
            //}
        }

        private void PopulateProductDetail()
        {
            DataSet ds = BAL.Customers.ViewProductDetail();
            gdProductDetail.DataSource = ds;
            gdProductDetail.DataBind();
        }



        protected void lnkView_Click(object sender, EventArgs e)
        {
            LinkButton lnkVw = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkVw.Parent.Parent;
            int ProductId = Convert.ToInt32(gdProductDetail.DataKeys[row.RowIndex]["ProductID"]);


            string[] Details = BAL.Customers.GetProductByID(ProductId);

            lblProductName.Text = Details[1];
            lblProducterName.Text = Details[2];
            lblDirectorName.Text = Details[3];
            lblRef.Text = Details[4];
            lnkView_ModalPopupExtender.Show();

        }
           

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            LinkButton lnkDel = (LinkButton)sender;
            GridViewRow row = (GridViewRow)lnkDel.Parent.Parent;
           // int rowIndex = Convert.ToInt32(lnkDel.CommandArgument);

            int ProductId = Convert.ToInt32(gdProductDetail.DataKeys[row.RowIndex ]["ProductID"]);

            BAL.Customers.IsDeleteProduct(ProductId);

           // PopulateProductDetail();
        }

        protected void lnkEdit_Click(object sender, EventArgs e)
        {
            LinkButton lnkEdit = (LinkButton)sender;
            GridViewRow row = (GridViewRow)lnkEdit.Parent.Parent;
          

            int ProductId = Convert.ToInt32(gdProductDetail.DataKeys[row.RowIndex]["ProductID"]);

            string[] details = BAL.Customers.GetProductByID(ProductId);

            lblPorductID.Text = details[0];
            txtProductName.Text = details[1];
            txtProducerName.Text = details[2];
            txtDirectorName.Text = details[3];
            txtRefference.Text = details[4];

            lnkEdit_ModalPopupExtender.Show();
   
        }



        protected void btnUpdate_Click(object sender, EventArgs e)
        {
           Button update = (Button)sender;
           // int rowIndex = Convert.ToInt32(update.CommandArgument);


         //int ProductId = Convert.ToInt32(gdProductDetail.DataKeys[RowIndex]["ProductID"]);

            //Panel gvPanel = (Panel)gdProductDetail.Rows[rowIndex].Cells[0].FindControl("pnlLnkEdit");

           // TextBox tProductName = (TextBox)gvPanel.FindControl("txtProductName");
           int ProductId =Convert.ToInt32( lblPorductID.Text);
           string ProductName = txtProductName.Text;

           // TextBox tProducerName = (TextBox)gvPanel.FindControl("txtProducerName");
           string ProducerName = txtProducerName.Text;

           // TextBox tDirectorName = (TextBox)gvPanel.FindControl("txtDirectorName");
           string DirectorName = txtDirectorName.Text;

          //  TextBox trefference = (TextBox)gvPanel.FindControl("txtEmail");
            string Refference = txtRefference.Text;


            BAL.Customers.UpdateProductDetail(ProductName, ProducerName, DirectorName, Refference, ProductId);

           // PopulateProductDetail();


        }



       
    }
}